// 弹窗配置
const alertConfig = {
    colors: {
        success: '#10B981',
        warning: '#F59E0B',
        error: '#EF4444',
        info: '#3B82F6',
        question: '#8B5CF6'
    },
    icons: {
        success: `<svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4z"/>
            <circle cx="12" cy="12" r="10" fill="transparent" stroke="currentColor" stroke-opacity="0.1" stroke-width="2"/>
        </svg>`,
        warning: `<svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 2L2 20h20L12 2z"/>
            <path fill="white" d="M11 15h2v2h-2v-2zm0-6h2v4h-2V9z"/>
            <circle cx="12" cy="12" r="10" fill="transparent" stroke="currentColor" stroke-opacity="0.1" stroke-width="2"/>
        </svg>`,
        error: `<svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M19 6.4L17.6 5 12 10.6 6.4 5 5 6.4 10.6 12 5 17.6 6.4 19 12 13.4 17.6 19 19 17.6 13.4 12z"/>
            <circle cx="12" cy="12" r="10" fill="transparent" stroke="currentColor" stroke-opacity="0.1" stroke-width="2"/>
        </svg>`,
        info: `<svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/>
            <circle cx="12" cy="12" r="10" fill="transparent" stroke="currentColor" stroke-opacity="0.1" stroke-width="2"/>
        </svg>`,
        question: `<svg viewBox="0 0 24 24" fill="currentColor">
            <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-4h2v2h-2zm1.61-9.96c-2.06-.3-3.61 1.14-3.61 2.96h2c0-.55.45-1 1-1s1 .45 1 1c0 .3-.1 1.25-.74 1.74-.64.5-1.26.76-1.26 1.26v1h2v-.67c0-.52.35-.97.84-1.33 1.48-1.12 1.16-2.95.87-3.67z"/>
            <circle cx="12" cy="12" r="10" fill="transparent" stroke="currentColor" stroke-opacity="0.1" stroke-width="2"/>
        </svg>`
    }
};

// 弹窗队列和状态
let alertQueue = [];
let isAlertActive = false;
let currentCallback = null;

// 显示基本弹窗
function showAlert(type, title, content) {
    alertQueue.push({ 
        type, 
        title, 
        content,
        isAdvanced: false
    });
    processAlertQueue();
}

// 显示高级弹窗
function showAdvancedAlert(options) {
    const defaultOptions = {
        type: 'info',
        title: '提示',
        content: '',
        buttons: [{ text: '确定', value: true, class: 'btn-primary' }],
        callback: null
    };
    
    alertQueue.push({
        ...defaultOptions,
        ...options,
        isAdvanced: true
    });
    
    processAlertQueue();
}

// 处理弹窗队列
function processAlertQueue() {
    if (isAlertActive || alertQueue.length === 0) return;
    
    isAlertActive = true;
    const alertData = alertQueue.shift();
    displayAlert(alertData);
}

// 显示弹窗
function displayAlert(data) {
    const container = document.getElementById('alertContainer');
    const icon = document.getElementById('alertIcon');
    const alertTitle = document.getElementById('alertTitle');
    const alertContent = document.getElementById('alertContent');
    const alertButtons = document.getElementById('alertButtons');
    
    // 设置弹窗内容
    icon.innerHTML = alertConfig.icons[data.type] || alertConfig.icons.info;
    alertTitle.textContent = data.title;
    if (typeof data.content === 'function') {
    alertContent.innerHTML = ''; // 清空旧内容
    const contentNode = data.content();
    alertContent.appendChild(contentNode);
    } else {
        alertContent.innerHTML = data.content;
    }
    
    // 设置颜色
    const color = alertConfig.colors[data.type] || '#3B82F6';
    document.documentElement.style.setProperty('--btn-color', color);
    
    // 清除之前的按钮
    alertButtons.innerHTML = '';
    
    // 定义关闭弹窗的函数
    const closeAlert = () => {
        container.classList.remove('active');
        setTimeout(() => {
            isAlertActive = false;
            processAlertQueue();
        }, 300);
    };
    
    if (data.isAdvanced) {
        // 高级弹窗 - 多按钮
        currentCallback = data.callback;
        
        data.buttons.forEach(btn => {
            const button = document.createElement('button');
            button.className = `ua-button ${btn.class || ''}`;
            button.textContent = btn.text;
            button.onclick = () => {
                closeAlert();
                if (typeof currentCallback === 'function') {
                    currentCallback(btn.value);
                }
            };
            alertButtons.appendChild(button);
        });
    } else {
        // 基本弹窗 - 单按钮
        const button = document.createElement('button');
        button.className = 'ua-button';
        button.textContent = '确定';
        button.onclick = closeAlert; // 直接引用 closeAlert
        alertButtons.appendChild(button);
    }
    
    // 添加事件处理
    container.onclick = (e) => {
        if (e.target === container && !data.preventClose) {
            closeAlert();
            if (data.isAdvanced && typeof currentCallback === 'function') {
                currentCallback(null);
            }
        }
    };
    
    // 设置弹窗类型属性
    container.setAttribute('data-type', data.type);
    
    // 显示弹窗
    container.classList.add('active');
}

// 初始化页面后显示欢迎弹窗或默默的在控制台输出日志，或者两个都显示
window.onload = () => {
    setTimeout(() => {
        console.log(`欢迎使用 UA-Alert(Ultimate Alert) 弹窗库！`);
        console.log(`作者: 小钻风的蛇作演讲`)
        console.log(`Bilibili: https://space.bilibili.com/2051233481`)
        console.log(`基于原始Scratch扩展"弹窗-圆粽子YuanZongzi"重构`)
        console.log(`设计：圆粽子YuanZongzi`)
        console.log(`Bilibili: https://space.bilibili.com/3546825153841333`)
        showAlert('info', '欢迎使用高级弹窗组件', '这是一个完全独立的弹窗解决方案，您可以在任何网页中使用它！<br>开源仓库: <a href="https://gitee.com/graduatedust800/ua-alert" target="_blank">UA-Alert</a><br>作者: <a href="https://space.bilibili.com/2051233481" target="_blank">小钻风的蛇作演讲</a></br>基于原始Scratch扩展"弹窗-圆粽子YuanZongzi"重构<br>设计：<a href="https://space.bilibili.com/3546825153841333" target="_blank">圆粽子YuanZongzi</a></br>');
    }, 1000);
};
